$(function () {
    init_create_selectUser();
    init_person_table();
    /**初始化时间选择**/
    $("#select_time span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#select_time').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        minDate: moment().add('year',-1), //设置最小日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#select_time span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("saleCustomerCreateTable");
    });
});
/**选择时间类型**/
function selectNySeletTimne(btn) {
    var data_cliak = $(btn).attr("data-click");
    if(data_cliak === "selft"){
        $("#select_time").removeClass("hidden");
        $("#starttime").val(moment().subtract("days",29).format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else{
        $("#select_time").addClass("hidden");
        $("#starttime").val(data_cliak);
        $("#endtime").val('');
    }
    $("#timeRemove").find("button").each(function () {
        $(this).removeClass("btn-primary");
    });
    $(btn).addClass("btn-primary");
    $JuXue.refreshTable("saleCustomerCreateTable");
}



/**初始化选择人员**/
function init_create_selectUser(){
    $.post(ctx+"dept/getUserAndDeptByTreeWhisRole",{},function (r) {
        var array  = r.msg;
        $("#select_sale_user").jstree({
            "core": {
                "multiple": true,//单选
                "data":array,
                "icon":false,
                "themes":{
                    "icons":false
                }
            },
            "checkbox" : {
                "keep_selected_style" : false
            },
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#select_sale_user').on("changed.jstree", function (e, data) {
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;;
            if (action == "deselect_node") {
                $("#otherUserSelect").find("div").each(function () {
                    var data_id = $(this).attr("data-id");
                    if (data_id == nodeid) $(this).remove();
                });
            }else{
                /**判断是不是超过 5个 超过5个则不能继续追加**/
                var divlength = $("#otherUserSelect").find("div");
                if(divlength.length >= 3){
                    $JuXue.n_danger("选择查询类型不能超过3个");
                    $("#select_sale_user").jstree("deselect_node",nodeid);
                    return;
                }else{
                    if(divlength.length >0){
                        var ble = false;
                        $("#otherUserSelect").find("div").each(function () {
                            var data_id = $(this).attr("data-id");
                            if (data_id == nodeid){ble = true;}
                        });
                        if(!ble){
                            var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";
                            $("#otherUserSelect").append(label);
                        }
                    }else{
                        var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";
                        $("#otherUserSelect").append(label);
                    }
                }
            }
        });
    });
}
/**初始化数据**/
function init_person_table(){
    var settings = {
        url: ctx + "PerSonCustomer/selectCusList",
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                myCusCreator : $("#gerenyuanid").val(),//个人资源录入人
                beforeTime: $("#starttime").val(), //开始时间
                afterTime:  $("#endtime").val(),//结束时间
                searchValue :$("#searchValue").val() //综合查询的值
            };
        },
        columns: [{
            checkbox: true,
            width: '5%',
        }, {
            field: 'myCusCreatetime',
            title: '创建时间'
        }, {
            field: 'myCusName',
            title: '客户名称',
            clickToSelect:false,
            formatter:function (value, row, index) {
                return "<a href='javascript:void(0)' onclick='viewMycusDetail(this);' uuid = '"+row.myCusUuid+"'  data-id='"+row.myCusId+"'>"+value+"</a>";
            }
        }, {
            field: 'myCusMobile',
            title: '手机'
        }, {
            field: 'myCusQq',
            title: 'QQ'
        }, {
            field: 'myCusWechat',
            title: '微信'
        },{
            field: 'myCusReceiver',
            title: '创建人'
        }]
    };
    $JuXue.initTable('saleCustomerCreateTable', settings);
}
/**选择员工**/
function selectUserList(){
    $('#selectsaleUsers').removeClass('open');
    var allUser = $('#select_sale_user').jstree(true).get_selected(false);//获得整个树
    var users = allUser.join(",");
    $("#gerenyuanid").val(users);
    $JuXue.refreshTable('saleCustomerCreateTable');
}
/**加载客户详情页面*/
function viewMycusDetail(btn) {
    var ids = $(btn).attr("data-id");
    $("#customer_uuid").val(ids);
    $("#customer_ids").val(ids);
    $.post(ctx+"myCustomer/loadCustomerDetail",{type:"4"},function (r) {
        $("#subcenterModal").empty().append(r).modal('show').draggable();
    });
}
